package com.example.aspect;

import com.example.annotation.Permission;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: zhugelongchen
 * @description:
 * @author: 诸葛龙辰
 * @create: 2022-11-13 19:48
 **/
@Component
@Aspect

public class PermissionAdvice {
    @Pointcut("@annotation(com.example.annotation.Permission)")
    public void permission(){
    }

    private List<String> permission=new ArrayList<String>();
    public  PermissionAdvice(){
        permission.add( "list");
        permission.add("delete");
        permission.add("update");
    }


    @Around("permission()")
    public Object before(ProceedingJoinPoint joinPoint) throws Throwable {
        //得到连接点执行的方法对象
        MethodSignature signature= (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        //得到方法上的注解
        Permission annotation = method.getAnnotation(Permission.class);
        if (annotation!=null){
            //获取注解属性的value值
            String value = annotation.value();
            if (!permission.contains(value)){
                return "quanxianbuzu";
            }
        }
        return joinPoint.proceed();
    }

}
